/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package converter;

import java.net.URI;
import jpa.entities.Endereco;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlAttribute;
import javax.ws.rs.core.UriBuilder;
import javax.persistence.EntityManager;
import jpa.entities.UsuarioCli;

/**
 *
 * @author carlos
 */

@XmlRootElement(name = "endereco")
public class EnderecoConverter {
    private Endereco entity;
    private URI uri;
    private int expandLevel;
  
    /** Creates a new instance of EnderecoConverter */
    public EnderecoConverter() {
        entity = new Endereco();
    }

    /**
     * Creates a new instance of EnderecoConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded@param isUriExtendable indicates whether the uri can be extended
     */
    public EnderecoConverter(Endereco entity, URI uri, int expandLevel, boolean isUriExtendable) {
        this.entity = entity;
        this.uri = (isUriExtendable) ? UriBuilder.fromUri(uri).path(entity.getIdEndereco() + "/").build() : uri;
        this.expandLevel = expandLevel;
        getUsuarioCli();
    }

    /**
     * Creates a new instance of EnderecoConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded
     */
    public EnderecoConverter(Endereco entity, URI uri, int expandLevel) {
        this(entity, uri, expandLevel, false);
    }

    /**
     * Getter for idEndereco.
     *
     * @return value for idEndereco
     */
    @XmlElement
    public Integer getIdEndereco() {
        return (expandLevel > 0) ? entity.getIdEndereco() : null;
    }

    /**
     * Setter for idEndereco.
     *
     * @param value the value to set
     */
    public void setIdEndereco(Integer value) {
        entity.setIdEndereco(value);
    }

    /**
     * Getter for logradouro.
     *
     * @return value for logradouro
     */
    @XmlElement
    public String getLogradouro() {
        return (expandLevel > 0) ? entity.getLogradouro() : null;
    }

    /**
     * Setter for logradouro.
     *
     * @param value the value to set
     */
    public void setLogradouro(String value) {
        entity.setLogradouro(value);
    }

    /**
     * Getter for numero.
     *
     * @return value for numero
     */
    @XmlElement
    public Integer getNumero() {
        return (expandLevel > 0) ? entity.getNumero() : null;
    }

    /**
     * Setter for numero.
     *
     * @param value the value to set
     */
    public void setNumero(Integer value) {
        entity.setNumero(value);
    }

    /**
     * Getter for bairro.
     *
     * @return value for bairro
     */
    @XmlElement
    public String getBairro() {
        return (expandLevel > 0) ? entity.getBairro() : null;
    }

    /**
     * Setter for bairro.
     *
     * @param value the value to set
     */
    public void setBairro(String value) {
        entity.setBairro(value);
    }

    /**
     * Getter for cidade.
     *
     * @return value for cidade
     */
    @XmlElement
    public String getCidade() {
        return (expandLevel > 0) ? entity.getCidade() : null;
    }

    /**
     * Setter for cidade.
     *
     * @param value the value to set
     */
    public void setCidade(String value) {
        entity.setCidade(value);
    }

    /**
     * Getter for uf.
     *
     * @return value for uf
     */
    @XmlElement
    public String getUf() {
        return (expandLevel > 0) ? entity.getUf() : null;
    }

    /**
     * Setter for uf.
     *
     * @param value the value to set
     */
    public void setUf(String value) {
        entity.setUf(value);
    }

    /**
     * Getter for cep.
     *
     * @return value for cep
     */
    @XmlElement
    public String getCep() {
        return (expandLevel > 0) ? entity.getCep() : null;
    }

    /**
     * Setter for cep.
     *
     * @param value the value to set
     */
    public void setCep(String value) {
        entity.setCep(value);
    }

    /**
     * Getter for usuarioCli.
     *
     * @return value for usuarioCli
     */
    @XmlElement
    public UsuarioCliConverter getUsuarioCli() {
        if (expandLevel > 0) {
            if (entity.getUsuarioCli() != null) {
                return new UsuarioCliConverter(entity.getUsuarioCli(), uri.resolve("usuarioCli/"), expandLevel - 1, false);
            }
        }
        return null;
    }

    /**
     * Setter for usuarioCli.
     *
     * @param value the value to set
     */
    public void setUsuarioCli(UsuarioCliConverter value) {
        entity.setUsuarioCli((value != null) ? value.getEntity() : null);
    }

    /**
     * Returns the URI associated with this converter.
     *
     * @return the uri
     */
    @XmlAttribute
    public URI getUri() {
        return uri;
    }

    /**
     * Sets the URI for this reference converter.
     *
     */
    public void setUri(URI uri) {
        this.uri = uri;
    }

    /**
     * Returns the Endereco entity.
     *
     * @return an entity
     */
    @XmlTransient
    public Endereco getEntity() {
        if (entity.getIdEndereco() == null) {
            EnderecoConverter converter = UriResolver.getInstance().resolve(EnderecoConverter.class, uri);
            if (converter != null) {
                entity = converter.getEntity();
            }
        }
        return entity;
    }

    /**
     * Returns the resolved Endereco entity.
     *
     * @return an resolved entity
     */
    public Endereco resolveEntity(EntityManager em) {
        UsuarioCli usuarioCli = entity.getUsuarioCli();
        if (usuarioCli != null) {
            entity.setUsuarioCli(em.getReference(UsuarioCli.class, usuarioCli.getIdCli()));
        }
        return entity;
    }
}
